<template>
  <div>
    <el-dialog
      title="编辑员工离职信息"
      :visible.sync="centerDialogVisible"
      width="30%"
      :close-on-click-modal="false"
      center
    >
      <div>
        <el-form ref="form" :model="form" label-width="100px" label-position="left">
          <el-form-item label="员工姓名">
            <span>{{name}}</span>
          </el-form-item>
          <el-form-item label="离职日期">
            <el-date-picker
              v-model="form.timer"
              @change="handleChange"
              type="date"
              placeholder="选择日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="离职申请表">
            <el-upload
              :headers="headers"
              class="avatar-uploader"
              action="/api/upload/uploadFile/3"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editClick">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { updateResign } from "@/service/Api";
export default {
  name: "editDialog.vue",
  data() {
    return {
      headers: {
        "API-User-Token": sessionStorage.getItem("token") //文件上传需携带token
      },
      centerDialogVisible: false,
      name: "",
      form: {
        timer: "",
        imageUrl: "",
        url: "",
        id: ""
      }
    };
  },
  methods: {
    showDialog(row) {
      console.log("离职员工信息", row);
      this.name = row.name;
      this.form.id = row.id;
      this.form.timer = row.quitTime;

      this.form.imageUrl = row.quitForm;
      console.log("新上传的图片", this.form.imageUrl);

      this.centerDialogVisible = true;
    },
    onSubmit() {
      console.log("submit!");
    },
    handleAvatarSuccess(res, file) {
      if (res.code === 0) {
        // this.form.url = res.data.fileIds;
        this.form.imageUrl = res.data.fileIds;
        // console.log(this.form.url);
        console.log(this.form.imageUrl);
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2000;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    editClick() {
      console.log(this.form.imageUrl);

      updateResign({
        uid: this.form.id,
        quitTime: this.form.timer,
        quitForm: this.form.imageUrl
      }).then(res => {
        if (res.code === 0) {
          this.$message.success("修改成功");
          this.centerDialogVisible = false;
          this.$emit("editSuccess", null);
        }
      });
    },

    handleChange(val) {
      const dayjs = require("dayjs");
      this.form.timer = dayjs(new Date(val)).format("YYYY-MM-DD");
    }
  }
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
